#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import time
import unittest
from selenium import webdriver
from poium import Page, Element


class LoginFrame(Page):
    lw_account_tab = Element(css='span[data-type="tuzhan"]')
    lw_input_user = Element(css='[placeholder="请输入你的登录账户"]')
    lw_input_pw = Element(css='[placeholder="请输入密码"]')
    lw_login_button = Element(css='button.g-btn.login-btn.do-btn')
    reg_err_tip = Element(css='div.login-form span.error-tips')


    def login(self, user, password):
        self.lw_account_tab.click()
        self.lw_input_user.input(user)
        self.lw_input_pw.input(password)
        self.lw_login_button.click()


class TestLoign(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.driver.get("https://www.rabbitpre.com/")
        self.nav_to_login_frame()

    def tearDown(self) -> None:
        self.driver.quit()

    def nav_to_login_frame(self):  # 打开切换到登录窗
        login_btn = self.driver.find_element_by_css_selector('#g-j-signin-btn')
        login_btn.click()
        login_iframe = self.driver.find_element_by_css_selector('#sso')
        self.driver.switch_to.frame(login_iframe)

    def test_wrong_passwrod(self):
        login_page = LoginFrame(self.driver)
        login_page.login("xxx", "xxx")
        time.sleep(5)
        self.assertEqual(login_page.reg_err_tip.text, "登录密码错误")

    def test_login_success(self):
        login_page = LoginFrame(self.driver)
        login_page.login("正解的用户名", "正确密码")
        time.sleep(5)
        self.assertIn("工作台", login_page.get_title)

